package com.example.user.myfirstapp.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.user.myfirstapp.MainActivity;
import com.example.user.myfirstapp.MyApplication;
import com.example.user.myfirstapp.R;
import com.example.user.myfirstapp.activity.LoginActivity;
import com.example.user.myfirstapp.common.Constant;
import com.example.user.myfirstapp.entity.UserInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 需要登录的fragment继承此类，以便于验证登录
 */
public class loginBaseFragment extends Fragment{
    private Intent intent;
    private SharedPreferences sp;
    private MainActivity mainActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainActivity =(MainActivity) getActivity();//MainActivity实例
        String key=Constant.sharedPreferences_user_key;
        try {
            //从sharedperferences里读取用户信息
            sp = getActivity().getSharedPreferences(key, Context.MODE_PRIVATE);
            String userJson = sp.getString("userJson", "");
            //如果sharedperferences里没有用户信息，跳转到登录页
            if (TextUtils.isEmpty(userJson)) {
                goToLogin();
            }
            else {
                Gson gson = new Gson();
                UserInfo user = gson.fromJson(userJson, new TypeToken<UserInfo>() {}.getType());
                String name=user.getUserName();
                String pwd=user.getUserPwd();
                //和服务器匹配，正确则自动登录，错误则跳转到登陆页
                String url = "http://"+ Constant.host+":"+Constant.port+"/api/UserInfo/GetUser/"+name+"/"+pwd;
                StringRequest request = new StringRequest(Request.Method.GET, url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String result) {
                                if (TextUtils.isEmpty(result) || result.equals("null")) {
                                    goToLogin();
                                }
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError err) {
                                Toast.makeText(getActivity(), "网络请求失败",
                                        Toast.LENGTH_LONG).show();
                            }
                        }
                );
                request.setTag("getUserByNamePwd");
                MyApplication.getHttpQueue().add(request);
            }
        }catch (Exception e){
        }
    }

    private void goToLogin(){
        intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
        //从下往上弹出
        getActivity().overridePendingTransition(R.animator.slide_bottom_in,0);
    }

    @Override
    public void onStop() {
        super.onStop();
        //结束时队列里面注销
        MyApplication.getHttpQueue().cancelAll("getUserByNamePwd");
    }
}
